<template>
  <div class="content_box">
    <div class="main-box">
      <p class="main-title">修改办案单位</p>
      <p class="main-text">注：取保时间一旦修改不可修改，日期请与文书一致后续可案件延期申请。</p>
      <el-form :model="form" :rules="rules" ref="updateForms">
        <el-form-item label="变更办案单位" label-width="110px" prop="department_id">
          <el-cascader
            style="width:90%"
            size="large"
            v-model="form.department_id"
            :options="bumenOptions"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item label="备注" label-width="110px" prop="notes">
          <el-input
            style="width:90%"
            size="large"
            v-model="form.notes"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button
          :loading="loading"
          :disabled="loading"
          type="primary"
          @click="submit"
        >
          提交
        </el-button>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { reactive, ref, defineProps,defineEmits,onMounted } from "vue";
import { ElNotification } from "element-plus";
import { getDepartmentList } from "@/api/public";
import { updateDepartment } from "@/api/bond/pay";

const props = defineProps({
  bailId: {
    type: Number,
  },
  id: {
    type: Number,
  },
});

let form = ref({
  process_id: props.id,
  department_id: "",
  notes: "",
});

const emit = defineEmits(['closeDrawer']);

onMounted(() => {
  getBumenList();
});

// 获取部门
let bumenOptions = ref([]);
let list = ref([]);
const getBumenList = async () => {
  await getDepartmentList().then((res) => {
    if (res.code == 1) {
      res.data = JSON.parse(
        JSON.stringify(res.data).replace(/department_id/g, "value")
      );
      res.data = JSON.parse(JSON.stringify(res.data).replace(/name/g, "label"));
      res.data = JSON.parse(
        JSON.stringify(res.data).replace(/child/g, "children")
      );
      bumenOptions.value = res.data;
      console.log(list.value);
    }
  });
};

/*=====提交=====*/
const updateForms = ref();
let loading = ref(false);
const rules = reactive({
  department_id: [
    { required: true, message: "请选择主办单位", trigger: "change" },
  ],
});

const submit = async () => {
  console.log(form.value);
  let obj = Object.assign({}, form.value);
  obj.department_id = `` + obj.department_id.at(-1);
  return
  await updateForms.value.validate();
  loading.value = true;

  await updateDepartment(obj).then((res) => {
    if (res.code == 1) {
      ElNotification({ message: res.msg, type: "success" });
      close()
    } else {
      loading.value = false;
      ElNotification.error(res.msg);
    }
  });
};

// 关闭
const close=()=>{
  form.value.department_id = ''
  form.value.notes = ''
  loading.value = false
  setTimeout(() => {
    updateForms.value.clearValidate();
  }, 500);
  
  emit("closeDrawer");
}
</script>
<style scoped lang="scss">
.dialog-footer {
  margin-top:50px;
  text-align: center;
}
.main-box{
  min-height: auto;
}
.main-text{
  margin:20px 0 47px 0;
  font-size: 14px;
  color: #323C4F;
}
</style>
  